<?php

namespace App\Http\Models;

use Illuminate\Database\Eloquent\Model;

class AccountsModel extends Model
{

	const PASSWORD_KEY = 'QOne30VNPoY&-dwH7/}G6E`]RzisWJx,1|_#q.4t';

	protected $connection = 'db_main';

	protected $table = 'wp_ucenter_member';

	protected $fillable = ["guid", "username", "password", "mobile", "reg_time", "reg_ip", "verify_mobile", "is_experience"];

	public $timestamps = false;

    public static function loginByEmail($email, $password) {
		$password = get_password($password, self::PASSWORD_KEY);
		return self::where(['email'=>$email, 'password' => $password, 'status' => 1]) ->first();
	}

	public static function loginByMobile($mobile, $password) {
		$password = get_password($password, self::PASSWORD_KEY);
		return self::where(['mobile'=>$mobile, 'password' => $password, 'status' => 1]) ->first();
	}

	// 检查手机号是否存在
	public static function checkMobile($mobile)
	{
		return self::where(['mobile'=>$mobile, 'verify_mobile' => 1]) ->first();
	}

    // 注册
	public static function register($mobile, $password, $reg_ip = '127.0.0.1') {
		return self::create([
			'guid' => GUID(),
			'username' => $mobile,
			'password' => get_password($password, self::PASSWORD_KEY),
			'mobile' => $mobile,
			'reg_time' => time(),
			'reg_ip' => ip2long($reg_ip),
			'verify_mobile' => 1,
			'is_experience' => 3,
			'status' => 1
		]);
	}

	// 更新密码
	public static function updatePwd($mobile, $password)
	{
		$password = get_password($password, self::PASSWORD_KEY);
		return self::where(['mobile' => $mobile, 'verify_mobile' => 1])
			->update(['password' => $password]);
	}
}

